<!--
@name:dialogConditionConfig
@author:Zhenglei
@date:10:32
@description:dialogConditionConfig
@update:2022/8/2610:32
-->
<template>
  <el-dialog title="条件判断配置"
             :visible.sync="visible"
             :before-close="modalClose"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="50%">
    <el-form :model="subData"
             label-position="right"
             label-width="180px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="使用到指征结果的指征：">
        <el-select v-model="subData.indicationResultIds"
                   class="w-300"
                   multiple
                   filterable
                   clearable
                   placeholder="请选择使用指征">
          <el-option v-for="item in pointerDataSource"
                     :key="item.id"
                     :label="item.indicationName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参与判断的指征：">
        <el-select v-model="subData.indicationValuesIds"
                   class="w-300"
                   multiple
                   filterable
                   clearable
                   placeholder="请选择参与指征">
          <el-option v-for="item in pointerDataSource"
                     :key="item.id"
                     :label="item.indicationName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="参与判断的条件：">
        <el-select v-model="subData.conditionIds"
                   class="w-300"
                   multiple
                   filterable
                   clearable
                   placeholder="请选择条件判断">
          <el-option v-for="item in conditionDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="参与判断的其他参数：">
        <el-select v-model="subData.paramsExtra"
                   class="w-300"
                   multiple
                   clearable
                   placeholder="请选择参数">
          <el-option v-for="item in parameterDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="慢病：">
        <el-select v-model="subData.diseaseId"
                   class="w-300"
                   clearable
                   filterable
                   placeholder="请选择慢病">
          <el-option v-for="item in diseaseDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="自定义结论：">
        <el-input class="w-200" v-model="addData.result" placeholder="请输入结论" clearable/>
        <el-select v-model="addData.status"
                   class="w-200"
                   clearable
                   placeholder="请选择状态">
          <el-option v-for="item in statusDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>

        <el-button type="primary"
                   @click="handleAdd()">新 增
        </el-button>
      </el-form-item>
      <el-form-item>
        <element-table
          :columns="columns"
          :data="subData.results">

          <template v-slot:option="{row,index}">
            <el-button type="danger"
                       @click="handleDel(index)">删除
            </el-button>
          </template>
        </element-table>

      </el-form-item>


      <el-form-item label="判断脚本：">
        <el-input class="w-300" type="textarea" autosize v-model="subData.scripts" placeholder="请输入判断脚本" clearable/>
      </el-form-item>
    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>

</template>

<script>

import ElementTable from "../../../../base/ElementTable";

export default {
  name: "dialogConditionConfig",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      subData: {
        indicationResultIds: [],
        indicationValuesIds: [],
        conditionIds: [],
        healthMapIndicationId: '',
        paramsExtra: [],
        results: [],
        scripts: '',
        diseaseId: '',
      },
      addData: {
        result: '',
        status: '',
      },
      columns: [
        {
          label: '结论',
          prop: 'result',
          align: 'center',
        },
        {
          label: '状态',
          prop: 'status',
          align: 'center',
          formatter: (row, cellValue) => {
            if (row.status === 0)
              return <el-tag type="danger">异常</el-tag>
            else if (row.status === 1)
              return <el-tag type="primary">正常</el-tag>
          }
        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true
        }
      ],
      pointerDataSource: [],
      conditionDataSource: [],
      diseaseDataSource: [],
      statusDataSource: [
        {
          name: '正常',
          id: 1
        },
        {
          name: '异常',
          id: 0
        }
      ],
      parameterDataSource: [
        {
          id: 'age',
          name: '年龄'
        },
        {
          id: 'sex',
          name: '性别'
        },
        {
          id: 'detectType',
          name: '检测方法学'
        },
        {
          id: 'clinostatism',
          name: '检测卧位'
        },
        {
          id: 'smoking',
          name: '吸烟'
        },
      ],

      formModelRules: {
        indicationId: [{required: true, message: '请选择指征', trigger: 'change'},],
        sort: [{required: true, message: '请输入排序', trigger: 'blur'},],
      }
    }
  },
  created() {
    this.subData.healthMapIndicationId = this.dataObj.id
    this.getDetail()
    this.getPointerList()
    this.getConditionList()
    this.getDiseaseList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    handleAdd() {
      if (!this.addData.result) {
        this.$message.warning('请输入结论')
      } else if (this.addData.status === '') {
        this.$message.warning('请选择状态')
      } else {
        this.subData.results.push(JSON.parse(JSON.stringify(this.addData)))
        this.addData.result = ''
        this.addData.status = ''
      }
    },
    handleDel(index) {
      this.subData.results.splice(index, 1)
    },
    handleSave() {
      this.post(this.api.healthMap.saveCondition, this.subData).then((res) => {
        if (res.data.code === '200') {
          this.$message.success('保存成功')
          this.$parent.getList()
          this.modalClose()
        }
      })
    },
    //查询详情
    getDetail() {
      this.post(this.api.healthMap.getConditionDetail, {
        healthMapIndicationId: this.dataObj.id,
      }).then((res) => {
        if (res.data.code === '200') {
          this.subData = Object.assign(this.subData, res.data.object)
          if (this.subData.indicationResultIds.length > 0) {
            this.subData.indicationResultIds = this.subData.indicationResultIds.map(item => parseInt(item))
          }
          if (this.subData.indicationValuesIds.length > 0) {
            this.subData.indicationValuesIds = this.subData.indicationValuesIds.map(item => parseInt(item))
          }
          if (this.subData.conditionIds.length > 0) {
            this.subData.conditionIds = this.subData.conditionIds.map(item => parseInt(item))
          }
        }
      })
    },
    //查询指征
    getPointerList() {
      this.loading = true
      this.post(this.api.healthMap.getIndicationList, {
        pageSize: 300,
      }).then((res) => {
        this.loading = false
        if (res.data.code === '200') {
          this.pointerDataSource = res.data.object
        }
      })
    },
    getDiseaseList() {
      this.post(this.api.promote.themeInfoList, {
        pageSize: 200,
        pageIndex: 0,
        type: 1
      }).then(res => {
        if (res.data.code === '200') {
          this.diseaseDataSource = res.data.object;
        }
      })
    },
    //查询条件
    getConditionList() {
      this.post(this.api.healthMap.getConditionList).then((res) => {
        if (res.data.code === '200') {
          this.conditionDataSource = res.data.object
        }
      })
    },
  },
  components: {
    'element-table': ElementTable,
  }
}
</script>

<style scoped>

</style>
